package com.cxj.magic;

import java.io.InputStream;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

import com.waps.AppConnect;

public class Main extends AppActivity {
    private Gallery gallery;
    private ImageAdapter imageAdapter;
    private ImageSwitcher imageSwitcher;
    private int lastPosition = minid;
    private static final int len = 121;
    public static final int minid = 2130837628;
    public static final int maxid = 2130837628 + len -1;
    private int[] resIds;
    private int[] resMinIds;

    private void setWallmap() {
        new AlertDialog.Builder(this).setTitle(R.string.app_about).setMessage(R.string.app_set_wallmap_msg)
                .setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        InputStream is = null;
                        try {
                            is = Main.this.getBaseContext().getResources().openRawResource(Main.this.resIds[Main.this.lastPosition]);
                            Main.this.setWallpaper(is);
                            Toast.makeText(Main.this, Main.this.getString(R.string.my_text_pre), 0).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            IOUtil.closeQuitely(is);
                        }
                    }
                }).setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        Toast.makeText(Main.this, Main.this.getString(R.string.my_text_no), 0).show();
                    }
                }).show();
    }

    public boolean onContextItemSelected(MenuItem paramMenuItem) {
        switch (paramMenuItem.getItemId()) {
        case 1:
            setWallmap();
            return true;
        case 2:
            View localView = getLayoutInflater().inflate(2130903040, null);
            final AlertDialog localAlertDialog = new AlertDialog.Builder(this).create();
            localAlertDialog.show();
            localAlertDialog.getWindow().setContentView(localView);
            localAlertDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramView) {
                    localAlertDialog.dismiss();
                }
            });
            return true;
        default:
            return super.onContextItemSelected(paramMenuItem);
        }
    }

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.main);

        initAdContext(this);

        this.resIds = new int[len];
        this.resMinIds = new int[len];
        for (int i = 0; i < len; i++) {
            this.resIds[i] = (2130837628 + i);
            this.resMinIds[i] = (0x7f020003 + i);
        }
        this.gallery = ((Gallery) findViewById(R.id.gallery));
        this.imageAdapter = new ImageAdapter(this);
        this.gallery.setAdapter(this.imageAdapter);
        this.gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
                Main.this.imageSwitcher.setImageResource(Main.this.resIds[(paramInt % Main.this.resIds.length)]);
                Main.this.lastPosition = (paramInt % Main.this.resIds.length);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        this.imageSwitcher = ((ImageSwitcher) findViewById(R.id.imageswitcher));
        this.imageSwitcher.setFactory(new ViewFactory() {
            @Override
            public View makeView() {
                ImageView localImageView = new ImageView(Main.this);
                localImageView.setBackgroundColor(-16777216);
                localImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                localImageView.setLayoutParams(new FrameLayout.LayoutParams(-1, -1));
                return localImageView;
            }
        });
        this.imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 17432576));
        this.imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 17432577));
        this.imageSwitcher.setLongClickable(true);
        this.imageSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                Intent localIntent = new Intent();
                localIntent.setClass(Main.this, ImgViewer.class);
                Bundle localBundle = new Bundle();
                localBundle.putInt("imgid", Main.this.resIds[Main.this.lastPosition]);
                localIntent.putExtras(localBundle);
                Main.this.startActivity(localIntent);
            }
        });

        startPointThread(this);
    }

    public void onCreateContextMenu(ContextMenu paramContextMenu, View paramView, ContextMenu.ContextMenuInfo paramContextMenuInfo) {
        super.onCreateContextMenu(paramContextMenu, paramView, paramContextMenuInfo);
        paramContextMenu.add(0, 1, 0, getString(R.string.btnSetWallmap));
        paramContextMenu.add(0, 2, 0, getString(R.string.btnAbout));
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (paramInt == 4) {
            new AlertDialog.Builder(this).setTitle("要退出" + getResources().getString(R.string.app_name) + "吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            paramDialogInterface.dismiss();
                            Main.this.finish();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            paramDialogInterface.dismiss();
                        }
                    }).create().show();
            return true;
        } else {
            return super.onKeyDown(paramInt, paramKeyEvent);
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        AppConnect.getInstance(this).finalize();
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        int mGalleryItemBackground;

        public ImageAdapter(Context arg2) {
            this.mContext = arg2;
            this.mGalleryItemBackground = arg2.obtainStyledAttributes(R.styleable.Gallery).getResourceId(0, 0);
        }

        public int getCount() {
            return 2147483647;
        }

        public Object getItem(int paramInt) {
            return Integer.valueOf(paramInt);
        }

        public long getItemId(int paramInt) {
            return paramInt;
        }

        public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
            ImageView localImageView = new ImageView(this.mContext);
            localImageView.setImageResource(Main.this.resMinIds[(paramInt % Main.this.resIds.length)]);
            localImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            localImageView.setLayoutParams(new Gallery.LayoutParams(120, 107));
            localImageView.setBackgroundResource(this.mGalleryItemBackground);
            return localImageView;
        }
    }
    
    public static int getValidPos(int id)
    {
        if(maxid >= id && minid <= id)
            return id;
        if(maxid < id)
            return minid;
        else 
            return maxid;
    }
}